import Vue from 'vue'
import Vuex from 'vuex'
// import mutations from './mutations'
import actions from './actions'
// import getters from './getters'

//安装插件
Vue.use(Vuex);
const state = {
    //登录状态
    isLogin: 0,
    user: {},
    token: localStorage.getItem('token') ? localStorage.getItem('token') : ''
};
//创建store对象
const store = new Vuex.Store({
    state,
    mutations: {
        //给state设置token值
        setToken(state, token) {
            state.token = token;
            localStorage.setItem('token', token)
        },
        //删除token值
        delToken(state) {
            state.token = '';
            localStorage.removeItem('token')
        },
        //改变登录状态
        changeStatus(state, status) {
            state.isLogin = status
        },
        //保存登录用户信息
        saveUser(state, payload) {
            state.user = payload
            localStorage.setItem('userInfo', JSON.stringify(payload))
        },
        //删除用户信息
        delUser(state) {
            state.user = {}
            localStorage.removeItem('userInfo')
        }
        // setToken(state,token){
        //     state.token = token
        //     localStorage.token = token
        // }
    },
    actions: {
        //设置token值
        SetToken({ commit }, token) {
            commit('setToken', token)
        },
        //改变登录状态
        ChangeStatus({ commit }) {
            commit('changeStatus', 1)
        },
        //保存用户信息
        SaveUserInfo({ commit }, userInfo) {
            commit('saveUser', userInfo)
        },
        //删除用户信息
        DelUserInfo({ commit }) {
            commit('delUser')
        }
    },
    getters: {
        user(state) {
            return state.user
        }
    }
});
export default store;